﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace Processor
{
   public class ProgramMapping
   {
      public string ProgramName;
      public int TVDBID;

      public ProgramMapping() { }
      public ProgramMapping(string scheduleName, int tvdbID)
      {
         this.ProgramName = scheduleName;
         this.TVDBID = tvdbID;
      }
   }

   public class FTRSettings
   {
      public Boolean Enabled = true;
      public string ServerName = "ftr";
      public int ServerPort = 49942;
      public Boolean UpdateFilename = false;
   }

   public class VRDSettings
   {
      public Boolean QSFEnabled = true;
      public string ProfileName = "H.264 MP4";
      public string NewFilenameExtension = ".mp4";
      public double VerifyThreshold = 0.70f;
      public Boolean EnableAdScan = true;
      public string EDLFilenameExtension = ".edl";
   }

   public class TVDBSettings
   {
      public Boolean RenameEnabled = true;
      public Boolean TwoDigitNumerics = true;
      public string RenamedFileFormat = "{Title} - s{SeasonNumber}e{EpisodeNumber} - {EpisodeName}";
   }

   public class Settings
   {
      public Boolean DeleteOriginalFile = false;

      public FTRSettings ForTheRecord;
      public VRDSettings VideoReDo;
      public TVDBSettings TVDB;

      public List<ProgramMapping> ProgramMappings;

      public static Settings CurrentSettings;

      static Settings()
      {
         if (File.Exists("Settings.xml"))
            CurrentSettings = Settings.Load("Settings.xml");
         else
            CurrentSettings = new Settings();
      }

      public Settings()
      {
         ForTheRecord = new FTRSettings();
         VideoReDo = new VRDSettings();
         TVDB = new TVDBSettings();

         ProgramMappings = new List<ProgramMapping>();

         // Example mapping:
         ProgramMappings.Add(new ProgramMapping("The King of Queens", 73641));
      }

      public int FindProgramMapping(string scheduleName)
      {
         foreach (ProgramMapping s in ProgramMappings)
            if (s.ProgramName.ToUpper().Trim() == scheduleName.ToUpper().Trim())
               return s.TVDBID;

         return -1;
      }

      public static Settings Load(string filename)
      {
         XmlSerializer xs = new XmlSerializer(typeof(Settings));
         TextReader tr = new StreamReader(filename);
         Settings s = (Settings)xs.Deserialize(tr);
         tr.Close();

         return s;
      }

      public void Save(string filename)
      {
         XmlSerializer xs = new XmlSerializer(typeof(Settings));
         TextWriter tw = new StreamWriter(filename);
         xs.Serialize(tw, this);
         tw.Close();
      }
   }
}
